<template>
	<el-dialog
	  v-model="dialog.visible"
	  :title="dialog.title"
	  destroy-on-close
		:close-on-click-modal="false"
	  append-to-body
	  width="650px"
	  class="custom-dialog"
	  @close="resetForm"
	> 

		<el-form
		  ref="userFormRef"
		  :model="formData"
		  :rules="userFormRules"
		  label-width="100px"
		>

			<el-form-item label="缩略图" prop="name">
				<el-upload
				  class="avatar-uploader"
					ref="upload"
					:auto-upload="false"
					:show-file-list="false"
					:on-change="handleChange"
				>
				  <img v-if="imageUrl" :src="imageUrl" class="avatar" width="256" height="160">
				  <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
				</el-upload>
			</el-form-item>
			
			<el-form-item label="行业分类" prop="classifyId">
				  <el-tree-select 
				  v-model="formData.classifyId" 
				  :props="props" 
				  :data="selectTreeData" 
				  :render-after-expand="true" 
				  style="width: 100%;" />
			</el-form-item>

			<el-form-item label="名称" prop="name">
			  <el-input v-model="formData.name" placeholder="请输入名称" />
			</el-form-item>
			
			<el-form-item label="软件包路径" prop="projectPath">
			  <el-input v-model="formData.projectPath" placeholder="请输入底板路径" />
			</el-form-item>
			<el-form-item label="exe路径" prop="exeRelativePath">
			  <el-input v-model="formData.exeRelativePath" placeholder="请输入底板exe对象对路径" />
			</el-form-item>
			
			<el-form-item label="预设分辨率" >
			  
			  <div class="form-row">
				  <div>
						<el-input-number
						  v-model="formData.resX"
						  :min="200"
						  :max="7680"
						  controls-position="right"
						/>
				  </div>
				  <div style="width: 20px;"></div>
				  <div>
					  <el-input-number
						v-model="formData.resY"
						:min="200"
						:max="7680"
						controls-position="right"
					  />
				  </div>
			  </div>
			</el-form-item>
			<el-form-item label="Fps" prop="fps">
			  <el-input-number placeholder="帧率" v-model="formData.fps" type="number" :min="24" :max="60" controls-position="right">
				<template slot="append">FPS</template>
			  </el-input-number>
			</el-form-item>
		</el-form>

		<template #footer>
		  <div class="dialog-footer">
			<el-button type="primary" @click="submitForm"  :loading="loading">确 定</el-button>
			<el-button @click="closeDialog" :loading="loading">取 消</el-button>
		  </div>
		</template>

	</el-dialog>
</template>

<script lang="ts" setup>
	
	import { saveForProject,selectTree} from "@/api/project";
	import { reactive, ref,defineEmits } from 'vue'
	import type {FormRules } from 'element-plus'
	import { Plus } from '@element-plus/icons-vue'

	const imageUrl = ref('');
	const loading = ref(false);
	const dialog = ref({
		visible:false,
		title:'',
	});
	
	const userFormRef=ref();
	const selectTreeData=ref([]);
	
	const userFormRules=reactive<FormRules>({
	  name: [{ required: true, message: "参数必填", trigger: "blur" }],
	  projectPath: [{ required: true, message: "参数必填", trigger: "blur" }],
	  exeRelativePath: [{ required: true, message: "参数必填", trigger: "blur" }],
	});
	
	const formData=reactive<Object>({
		id:'',
		classifyId:'',
		name:'',
		resX:1920,
		resY:1080,
		fps:25,
		thumbnail:'',
		projectPath:'',
		exeRelativePath:'',
		thumbnailFile:null,
	})

	const props = {
	  label: 'name',
	  value:'id',
	  children: 'children',
	}

	const loadSelectTree=()=>{
		selectTree({id:-1})
		.then((res) => {
			selectTreeData.value=res.result
		})
		.catch((e) => {
		})
		.finally(() => {
			loading.value=false;
		});
	}

	const handleChange=(file, fileList)=> {
		formData.thumbnailFile=file.raw;
		imageUrl.value = URL.createObjectURL(file.raw);
	}

	const openDialog=(row:object,type:string)=>{
		loadSelectTree();
		if('add'==type){
			dialog.value.visible=true;
			dialog.value.title='新增';
			Object.assign(formData, {pid:row?row.id:'-1'});
		}else{
			dialog.value.visible=true;
			dialog.value.title='编辑';
			if(row.id){
			  imageUrl.value=import.meta.env.VITE_APP_BASE_API+'/api/v1/thumbnail/'+row.id
			}
			Object.assign(formData, {
				id:row.id,
				classifyId:row.classifyId,
				name:row.name,
				resX:row.resX,
				resY:row.resY,
				fps:row.fps,
				thumbnail:row.thumbnail,
				projectPath :row.projectPath,
				exeRelativePath:row.exeRelativePath
			});
			
		}
	}
	const submitForm=()=>{
		userFormRef.value.validate((isValid: boolean) => {
		  if (isValid) {
				loading.value=true;
				saveForProject(formData)
				.then((res) => {
					ElMessage.success(res.message);
					closeDialog();
				})
				.catch((e) => {
				})
				.finally(() => {
					loading.value=false;
				});
		  }
		});
	}
	
	const emits = defineEmits(['closeDialog'])
	const closeDialog=()=>{
		dialog.value.visible=false;
		dialog.value.title="";
		emits('closeDialog',formData)
		resetForm();
	}
	
	function resetForm() {
	  userFormRef.value.resetFields();
	  userFormRef.value.clearValidate();
	  formData.id = "";
	  formData.classifyId = "";
	  formData.name= "";
	  formData.resX= 1920
	  formData.resY = 1080;
	  formData.fps = 25;
	  formData.thumbnail = "";
	  formData.projectPath = "";
	  formData.exeRelativePath = "";
	  formData.thumbnailFile=null;
	  imageUrl.value='';
	}	
	
	defineExpose({
	  openDialog,closeDialog
	});
	
	onMounted(() => {
	});
	onBeforeUnmount(() => {
	});
</script>

<style scoped>
	
	:deep(.avatar-uploader .el-upload){
	  border: 1px dashed #ccc;
	  border-radius: 6px;
	  cursor: pointer;
	  position: relative;
	  overflow: hidden;
	  width: 16rem;
	  height: 10rem;
	}
	
	.avatar-uploader .el-upload:hover {
	  border-color: var(--el-color-primary);
	}
	
	.el-icon.avatar-uploader-icon {
	  font-size: 28px;
	  color: #8c939d;
	  width: 178px;
	  height: 178px;
	  text-align: center;
	}
	
	.form-row{
		width: 100%;
		display: flex;
		flex-direction: row;
		padding-right: 0.625rem;;
	}
	
</style>